<?
session_start();

include('./../config/db_settings.php');
include('pms_inc.php');

$user = $_SESSION[$settings['session_prefix'].'user_id'];
//echo '$user:'.$user;
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
<head>
<title>Private Message Center</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../templates/<? echo $settings['template'] ?>/style.css" media="all" />
</head>
<script>
function checkUncheckAll(theElement) 
{
   var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length; z++)
	 {
   		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
   		{
	 			theForm[z].checked = theElement.checked;
	 		}
   }
}

function del_confirm(theElement)
{
	 var theForm = theElement.form;
	 var z = 0;
	 var ifChecked = 0;
	 for(z=0; z<theForm.length; z++)
	 {
   		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
   		{
	 			if(theForm[z].checked)
	 			{
	 				ifChecked=1;
	 				break;
	 			}
	 		}
   }
  if(!ifChecked)
  {
  	alert("Please select at least one message that you want to delete.");
  	return false;
  }      
  
var r=confirm("Are you sure you want to delete those selected message(s) permanently?");
if (r==true)
  {
  return true;
  }
else
  {
  return false;
  }
}
</script>  
<body>
<?     
    
    //Are they logged in or not?
    if(!$user)
        {
        echo "<br><p>You aren't logged in. Please log in first.</p><br>";
        }
        
    else
        {
        //Get your private message count
        $sql = mysql_query ("SELECT count(*) pm_count FROM ".$db_settings['messages_table']." WHERE sender_id='$user' and received in (0,1,4,5)");
        $row = mysql_fetch_array ($sql);
        $pm_count = $row['pm_count'];
        
        $percent = $pm_count/'50';
        $percent = $percent * '100';
        ?>
        <br>
        <center>
        <b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
        <b><p>Sentbox: <? echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
        </center>
        <br>
        
        <?
        //Get all message that are sent by the logged in user
        $query = "SELECT a.*, b.user_name receiver FROM ".$db_settings['messages_table']." a join ".$db_settings['userdata_table']." b on a.receiver_id=b.user_id WHERE a.sender_id='$user' and received in (0,1,4,5) order by a.id desc";
        
        $sqlsentbox = mysql_query($query);
        
        //If there is a MySQL error we need to display the error because something has gone horribly wrong on the server.
        if(!$sqlsentbox)
            {
            ?>
            <p><? print '$query: '.$query.mysql_error();?></p>
            <?
            }
        
        //If all messages sent by the user were deleted, then just say that there are no messages  
        elseif (!mysql_num_rows($sqlsentbox) )
            {
            ?>
            <center><p><b>You have not sent any messages or all the messages have been deleted.</b></p></center>
            <?
            }
        
        //If there are messages that have not been received yet we need to display those messages to the user
        else
            {
            //Lets make a table for the messages to go in, to keep things organised and such
            ?>
            <form name="frm_1" id="frm_1" method="post" action="delete.php">
            <input type="hidden" name="sender_id" value="<? echo $user ?>">
            <table class="normaltab" border="0" cellpadding="5" cellspacing="1" width="80%">
              <tr>
                  <th width="" valign="top">Subject</th>
                  <th width="120px" valign="top">Receiver</th>
        		  		<th width="150px" valign="top">Time</th>
        		  		<th width="25px" valign="top">Select</th>
              </tr>
            <?
            //This is a while loop that goes through the array while there are still things in it, getting the subject and receiver and displaying them
            $i = 1;
		    		$c = "a";
            while($sentbox = mysql_fetch_array($sqlsentbox))
                {
                $receiver = $sentbox['receiver'];
                $subject = htmlspecialchars(stripslashes(base64_decode($sentbox['subject'])));
                $pm_id = $sentbox['id'];
                $receiver_id = $sentbox['receiver_id'];
                $received = $sentbox['received'];
			    			$pm_ts = $sentbox['pm_ts'];
			    			$body = $sentbox['message'];
			    			
                if($i==1){$c = "a";}
    			else {$c = "b";}
    			$i= $i * -1;
                ?>
                <tr>
                  <td valign="top" class="<? echo $c; ?>"><span class="small"><? if($received=='0' || $received=='2'){ ?><img src='../templates/<? echo $settings['template'] ?>/images/email.png'><? } ?> <a href="viewmsg.php?msg_id=<? echo $pm_id; ?>"><? echo $subject; ?> <? if(strlen($body)>0){ ?><img src='../templates/<? echo $settings['template'] ?>/images/pages.png'><? } ?></a></span></td>
                  <td valign="top" class="<? echo $c; ?>"><span class="small"><span class="small"><a href = "<? echo "../index.php?mode=user&show_user=$receiver_id"; ?>" target="htkou_showuser"><? echo "$receiver"; ?></a></span></td>
                  <td valign="top" class="<? echo $c; ?>"><span class="small"><? echo $pm_ts; ?></span></td>
                  <td valign="top" class="<? echo $c; ?>"><input name="pms[]" type="checkbox" value="<? echo $pm_id; ?>"></td>  
                </tr>
                <?
                }?>
                <tr>  
								<td colspan="3" class="<? echo $c; ?>" align='right'><input type="submit" name="Submit" value="Delete Selected" onclick="return del_confirm(this)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select/unselect all</td>
								<td class="<? echo $c; ?>"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></td>
								</tr>
            </table>
            </from>
            <?
            }
        }    
    ?>
</body>
</html>